package com.aleaho.cftrack.presenter;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.db.gen.LastLoginDao;
import com.aleaho.cftrack.db.model.LastLogin;
import com.aleaho.cftrack.db.service.LastLoginService;
import com.aleaho.cftrack.db.service.UserService;
import com.aleaho.cftrack.http.entity.LoginEntity;
import com.aleaho.cftrack.utils.JsonTool;
import com.aleaho.cftrack.view.SplashView;

import java.util.List;

/**
 * Created by Administrator on 2017/10/8.
 */

public class SplashPresenter {

    private SplashView splashView = null;
    private CFTrackApplication app = null;
    private LoginEntity loginEntity = null;

    public SplashPresenter(SplashView splashView, CFTrackApplication application) {
        this.splashView = splashView;
        this.app = application;
    }


    public void checkLoginStatus() {

        String lastLoginUserName = LastLoginService.getLastLoginUserName();
        if (lastLoginUserName == null) {
            this.splashView.turnToLogin();
            return;
        }

        String userinfo = UserService.getUserInfo(lastLoginUserName);

        if (userinfo == null) {
            this.splashView.turnToLogin();
            return;
        }

        LoginEntity loginEntity = JsonTool.jsonToObject(userinfo, LoginEntity.class);

        if (loginEntity != null && loginEntity.getUserToken().getKey().length() == 40) {
            this.app.setLoginEntity(loginEntity, false);
            this.splashView.turnToMain();
            return;

        } else {

            this.splashView.turnToLogin();
            return;
        }
    }


}
